/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.luck.pf.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinUtils {

	public static String toPinyin(String chineseText) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat() ;
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		StringBuffer buffer = new StringBuffer();
		try{
			for(int i = 0;i < chineseText.length();i++) {
				char c = chineseText.charAt(i);
				String[] a = PinyinHelper.toHanyuPinyinStringArray(c, format);
				if(a == null) {
					buffer.append(c);
				}else {
					buffer.append(a[0]);
				}
				
			}
			return buffer.toString();
		}
		catch(BadHanyuPinyinOutputFormatCombination e) {
			throw new RuntimeException(e);
		}
		
		
	}
	
	public static void main(String[] args) {
		System.out.println(toPinyin("中华人民共和国驴abc"));
	}
}
